package com.collectplus.express.app;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;

public abstract class BasePopupWindow {

	private BaseActivity2 context;

	private PopupWindow popupPindow;

	/**
	 * 内容layout
	 */
	private View layout;

	private int animStyle;
	
	/**
	 * 
	 * @param context
	 * @param animStyle {@link R.style#popup_window_bottom2top}
	 */
	public BasePopupWindow(BaseActivity2 context, int animStyle) {
		this.context = context;
		this.animStyle  = animStyle;
		findViewById();
	}

	public void setContentView(int layoutResource) {
		layout = LayoutInflater.from(context).inflate(layoutResource, null, false);
		this.popupPindow = createPopupWindow(context, layout);
	}

	public View findViewById(int id) {
		return layout.findViewById(id);
	}

	public void showAsDropDown(int anchor) {
		View anchorView = context.findViewById(anchor);
		popupPindow.showAsDropDown(anchorView);
	}

	/**
	 * 相对于整个屏幕
	 * 
	 * @param parent
	 *            只要是屏幕中的view即可
	 * @param gravity
	 * @param x
	 *            x、y根据gravity的不同意义也不同,比如 gravity 设置为 {@link Gravity#TOP | Gravity#RIGHT} 那么x即使相对于top, y即使相对于right
	 * @param y
	 */
	public void showAtLocation(View parent, int gravity, int x, int y) {
		popupPindow.showAtLocation(parent, gravity, x, y);
	}

	protected abstract void findViewById();
	
	
	public void dismiss(){
		this.popupPindow.dismiss();
	}

	public BaseActivity2 getContext(){
		return context;
	}
	
	public PopupWindow createPopupWindow(final Activity context, View layout) {
		PopupWindow mpWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mpWindow.setOutsideTouchable(true);// popupwindow设置触摸可消失
		// ColorDrawable dw = new ColorDrawable(android.R.color.black);
		// dw.setAlpha(180);
		// mpWindow.setBackgroundDrawable(dw); // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		// popupwindow显示方式
		// mpWindow.showAtLocation(context.findViewById(R.id.address_call), Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
		mpWindow.setFocusable(true);// 这里必须设置为true才能点击区域外或者消失
		mpWindow.setTouchable(true);// 这个控制PopupWindow内部控件的点击事件
		mpWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mpWindow.setOutsideTouchable(true);
		mpWindow.setAnimationStyle(animStyle);
		mpWindow.update();
		// 设置背景变暗
		final Window window = context.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = 0.7f;
		window.setAttributes(lp);
		window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		// context.findViewById(android.R.id.content).setAlpha(0.7F);
		mpWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
			@Override
			public void onDismiss() {
				resetWindowAttribute(context);
			}
		});
		return mpWindow;
	}

	private void resetWindowAttribute(Activity context) {
		WindowManager.LayoutParams lp = context.getWindow().getAttributes();
		lp.alpha = 1f;
		context.getWindow().setAttributes(lp);
		context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}
}
